05. Primitives vs Objects

Primitives vs Objects

ND079 C1 L1 A04a Primitives Vs Objects

Primitive Values vs Reference Values

In Java, there are two general kinds of values we can assign to a variable:

  • A primitive value is simply a value, by itself, with no additional data.
  • A reference value is a value that refers to an object stored in another location in memory.

Objects bundle the primitive value up with additional useful information and behavior. We'll get into how to create and use objects more later on.

Primitive Types

ND079 C1 L1 A04b Primitive Types

In Java, there are eight primitive types, as you can see here:

Data Type Size
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
boolean 1 byte
char 2 bytes

As you can see in the table, each primitive type has a memory size allocation, meaning that if we created a variable with one of these types, that variable would have a certain size in memory (e.g., an int primitive is always allocated 4 bytes in memory).

The syntax for defining a primitive variable is simply an assignment statement like our example from earlier:

int age = 42;

Here, the integer 42 is a primitive value.

To declare a variable for the long primitive, we would simply type something like:

long agePlantEarth = 4005000000;

Notice that all of the keywords for primitives start with a lowercase letter.

Object Reference Types

ND079 C1 L1 A04c Object Types

As we said above, reference types create a reference to a data object. This object can be a Java defined type, like String or Array or it can be a customized, user-defined object. This allows literally infinite flexibility, since you can define whatever object types you need for your particular application.

Unlike primitive types, object types do not have a specific memory allocation size. The reference to the object can be of a known, fixed size, but the object itself may vary greatly in size (e.g., the string "hi" will be allocated less space than the string "abcdefghijklmnopqrstuvwxyz").

The syntax for declaring a String object variable looks like this:

String text = “Hello there”;

Or if we want to create an object variable for an integer, that looks like this:

Integer age = 42;

Notice that the keywords for creating object variables begin with an uppercase letter (String and Integer) rather than the lowercase used for primitives (e.g. int and long).

Again, one of the benefits of creating an object is that it can include additional data and behavior. For example, String objects are bundled with a method called length that lets us get the length of the string. For example:

String text = “Hello there”;
text.length();

This second line would give us the length of the string "Hello there", which is 11 characters long.

QUIZ QUESTION::

See if you can correctly indicate whether each of these keywords is used for a primitive or an object reference.

ANSWER CHOICES:



Keyword

Primitive or Reference

int

String

char

double

Integer

SOLUTION:

Keyword

Primitive or Reference

String

Integer

int

char

double

int

char

double

String

Integer

int

char

double